package com.wlyuan.open.scanner.repository.impl;

import com.wlyuan.open.scanner.domain.quotation.QuotationUpDown;
import com.wlyuan.open.scanner.repository.OrderCombineRepository;
import com.wlyuan.open.scanner.repository.TransportRepository;
import com.wlyuan.open.scanner.utils.enums.order.OrderSrcClassEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author lijing
 * @description
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class QuotationUpDowRepository {
    private final OrderCombineRepository orderCombineRepository;
    private final TransportRepository transportRepository;

    public QuotationUpDown upAndDown(Long orderId, Long srcClass) {
        if (OrderSrcClassEnum.COMBINE_ORDER.getValue().equals(srcClass)) {
            //合单上下游
            var orderCombine = orderCombineRepository.getById(orderId);
            return Optional.ofNullable(orderCombine).map(combine -> {
                QuotationUpDown quotationUpDown = new QuotationUpDown();
                quotationUpDown.setCarrierType(combine.getCarrierType());
                quotationUpDown.setCarrierName(combine.getCarrierName());
                quotationUpDown.setCarrierId(combine.getCarrierId());
                quotationUpDown.setUpTenantId(combine.getCarrierTenantId());
                quotationUpDown.setCustomerName(combine.getCustomerName());
                quotationUpDown.setCustomerType(combine.getCustomerType());
                quotationUpDown.setDownTenantId(combine.getCustomerTenantId());
                quotationUpDown.setCustomerId(combine.getCustomerId());
                /*//承运方为线下或者自营 上游发起方
                quotationUpDown.setCarrierIssue(true);
                quotationUpDown.setCustomerIssue(false);
                //承运方为线上 下游发起方
                if (CarrierTypeEnum.isOnline(quotationUpDown.getCarrierType())) {
                    quotationUpDown.setCarrierIssue(false);
                    quotationUpDown.setCustomerIssue(true);
                }*/
                return quotationUpDown;
            }).orElse(null);
        } else {
            //运单上下游
            var transport = transportRepository.getCarrierCustomer(orderId);
            return Optional.ofNullable(transport).map(t -> {
                QuotationUpDown quotationUpDown = new QuotationUpDown();
                Optional.ofNullable(t.getCarrier()).ifPresent(upStream -> {
                    quotationUpDown.setCarrierType(upStream.getCarrierType());
                    quotationUpDown.setCarrierName(upStream.getCarrierName());
                    quotationUpDown.setCarrierId(upStream.getCarrierId());
                    quotationUpDown.setUpTenantId(upStream.getTenantId());
                });
                Optional.ofNullable(t.getCustomer()).ifPresent(downStream -> {
                    quotationUpDown.setCustomerName(downStream.getCustomerName());
                    quotationUpDown.setCustomerType(downStream.getCustomerType());
                    quotationUpDown.setDownTenantId(downStream.getTenantId());
                    quotationUpDown.setCustomerId(downStream.getCustomerId());
                });
                /*//运输单 或者 客户单（线下客户）  上游发起方
                quotationUpDown.setCarrierIssue(true);
                quotationUpDown.setCustomerIssue(false);
                //客户单 线上客户 下游发起方
                if (OrderSrcClassEnum.isOrder(srcClass) && CustomerTypeEnum.isOnline(quotationUpDown.getCustomerType())) {
                    quotationUpDown.setCarrierIssue(false);
                    quotationUpDown.setCustomerIssue(true);
                }*/
                return quotationUpDown;
            }).orElse(null);
        }
    }
}
